<?php
/**
 * 获取延期配置详情
 * User: Administrator
 * Date: 2018/8/5/005
 * Time: 20:25
 */

namespace app\lib\Order\OrderExtensionConfig;


use app\model\OrderExtensionConfigModel;

class GetExtensionConfig
{
    protected  $extensionId;
    protected  $model;
    protected  $cache;
    protected  $cacheName;
    public function __construct($extensionId,$cache=true)
    {
        if(empty($extensionId) || !is_numeric($extensionId) || $extensionId<0 ){
            exception('参数错误');
        }
        $this->extensionId = $extensionId;
        $this->cacheName = md5('OrderExtensionId_'.$extensionId.'_ConfigInfo');
    }

    /**
     * 判断延期配置是否存在并且可以使用
     * @return bool
     */
    public function isExistAndUse(){
        if($this->cache){
            $result = $this->getCache();
            $result = isset($result['id'])&&!empty($result['id'])?$result['id']:false;
        }
        if(!$this->cache){
            $model = $this->getTable();
            $map = [$model->_id = $this->extensionId , $model->_status = 1];
            $result = $model->where($map)->value($model->_id);
        }
        return empty($result)?false:true;
    }

    /**
     * 获取配置时长
     * @return bool
     */
    public function getConfigTime(){
        if($this->cache){
            $result = $this->getCache();
            $result =  isset($result['configTime'])?$result['configTime']:0;
        }
        if(!$this->cache){
            $model = $this->getTable();
            $map = [$model->_id = $this->extensionId , $model->_status = 1];
            $result = $model->where($map)->value($model->_configTime);
        }
        return $result;
    }

    /**
     * 获取配置详情
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getConfigInfo(){
        if($this->cache){
            $data = $this->getCache();
        }else{
            $model =  $this->getTable();
            $map   =  [$model->_id=>$this->extensionId];
            $data  =  $model->where($map)->find();
        }
        return $data;
    }

    /**
     * 获取配置缓存
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     */
    protected function getCache(){
        $cacheData = cache($this->cacheName);
        if($cacheData){
            $data =   json_decode($cacheData,true);
        }
        if(!$cacheData){
            $data = $this->generateCache();
        }
        return $data;
    }

    /**
     * 获取配置表
     * @return OrderExtensionConfigModel
     */
    protected function getTable(){
        if(!$this->model){
           $this->model = new OrderExtensionConfigModel();
        }
        return $this->model ;
    }

    /**
     * 生成配置详情
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function generateCache(){
        $this->cache = false;
        $data =  $this->getConfigInfo();
        cache($this->cacheName,json_encode($data),604800);
        return $data;
    }

    /**
     * 清除配置
     * @return bool
     */
    public function delCache(){
        cache($this->cacheName,null);
        return true;
    }
}